package com.xindaoapp.happypet.shop.ui;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.kmshack.newsstand.ScrollTabHolderFragment;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.NoDataAdapter;
import com.xindaoapp.happypet.bean.MoreProduct;
import com.xindaoapp.happypet.bean.Product;
import com.xindaoapp.happypet.entry.CommentInfos;
import com.xindaoapp.happypet.entry.GoodsInfos;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;

public class ProductDetailFragment extends ScrollTabHolderFragment implements OnScrollListener {

    private static final String ARG_POSITION = "position";

    private ListView mListView;
    private ArrayList<String> mListItems;

    private int mPosition;

    private Product mProductInfos;
    private GoodsInfos mGoodsInfos;
    private CommentInfos mCommentInfos;

    // 更多布局
    private LinearLayout more;

    private View no_result;



    public static Fragment newInstance(int position, Product productInfos, GoodsInfos goodsInfos,
            CommentInfos commentInfos) {
        ProductDetailFragment f = new ProductDetailFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        b.putSerializable(Constants.KEY_PRODUCT_DETAIL_INFO, productInfos);
        b.putSerializable(Constants.KEY_PRODUCT_INTRO_INFO, goodsInfos);
        b.putSerializable(Constants.KEY_PRODUCT_COMMENTINFOS, commentInfos);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPosition = getArguments().getInt(ARG_POSITION);
        mProductInfos = (Product) getArguments().getSerializable(Constants.KEY_PRODUCT_DETAIL_INFO);
        mGoodsInfos = (GoodsInfos) getArguments().getSerializable(Constants.KEY_PRODUCT_INTRO_INFO);
        mCommentInfos =
                (CommentInfos) getArguments().getSerializable(Constants.KEY_PRODUCT_COMMENTINFOS);

        mListItems = new ArrayList<String>();

        // for (int i = 1; i <= 100; i++) {
        // mListItems.add(i + ". item - currnet page: " + (mPosition + 1));
        // }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_product_detail, null);

        mListView = (ListView) v.findViewById(R.id.listview);

        View placeHolderView = inflater.inflate(R.layout.view_header_placeholder, mListView, false);
        mListView.addHeaderView(placeHolderView);

        View footView = inflater.inflate(R.layout.product_detail_more, mListView, false);
        more = (LinearLayout) footView.findViewById(R.id.product_detail_more);
        List<MoreProduct> more = null;
        if (mProductInfos != null) {
            more = mProductInfos.introduce_goods;
        } else {
            more = new ArrayList<MoreProduct>();
        }
        if (more.size() > 0) {
            prepareMore(more);
        } else {
            footView.findViewById(R.id.l_buy_more).setVisibility(View.GONE);
        }
        mListView.addFooterView(footView);

        no_result = v.findViewById(R.id.no_result);
        return v;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        mListView.setOnScrollListener(this);

        if (mGoodsInfos != null) {
            if (mPosition == 0) {
                mListView.setAdapter(new ProductDetailAdapter(0, getActivity(), mGoodsInfos.data,
                        mProductInfos, mCommentInfos));
            } else if (mPosition == 1) {
                mListView.setAdapter(new ProductDetailAdapter(1, getActivity(), mGoodsInfos.data,
                        mProductInfos, mCommentInfos));
            } else if (mPosition == 2) {
                if (mCommentInfos != null && mCommentInfos.data != null
                        && mCommentInfos.data.info != null) {
                    no_result.setVisibility(View.GONE);
                    // for (int i = 0; i < 10; i++) {
                    // mCommentInfos.data.info.addAll(mCommentInfos.data.info);
                    // }
                    ProductDetailCommentAdapter productDetailCommentAdapter =
                            new ProductDetailCommentAdapter(mProductInfos.goods_id, getActivity(),
                                    mCommentInfos.data.info, 10,
                                    R.layout.list_item_product_comment, R.layout.item_loading);
                    mListView.setAdapter(productDetailCommentAdapter);
                } else {
                    NoDataAdapter nodataAdapter = new NoDataAdapter();
                    mListView.setAdapter(nodataAdapter);
                    // no_result.setVisibility(View.VISIBLE);
                }
            }
        } else {
            NoDataAdapter nodataAdapter = new NoDataAdapter();
            mListView.setAdapter(nodataAdapter);
        }

    }

    @Override
    public void adjustScroll(int scrollHeight) {
        if (scrollHeight == 0 && mListView.getFirstVisiblePosition() >= 1) {
            return;
        }

        mListView.setSelectionFromTop(1, scrollHeight);

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        if (mScrollTabHolder != null)
            mScrollTabHolder.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount,
                    mPosition);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {}

    private void prepareMore(final List<MoreProduct> info) {
        more.removeAllViews();
        for (int i = 0; i < info.size(); i++) {
            View view = View.inflate(getActivity(), R.layout.product_detail_more_data, null);
            ImageView iv = (ImageView) view.findViewById(R.id.more_img);
            TextView title = (TextView) view.findViewById(R.id.more_title);
            TextView price = (TextView) view.findViewById(R.id.more_price);

            int dip2px = CommonUtil.dip2px(getActivity(), 10);

            int width = getWidth();
            LinearLayout.LayoutParams lp =
                    new LinearLayout.LayoutParams((width - dip2px * 4) / 3,
                            (width - dip2px * 4) / 3);
            title.setWidth((width - dip2px * 4) / 3);
            ImageLoader.getInstance().displayImage(info.get(i).goods_thumb, iv);
            iv.setPadding(1, 1, 1, 1);
            iv.setBackgroundColor(Color.parseColor("#D1D0D4"));
            iv.setLayoutParams(lp);
            iv.setScaleType(ScaleType.FIT_XY);
            title.setText(info.get(i).goods_name);
            price.setText("￥" + formatDaZhe(Float.valueOf(info.get(i).shop_price)));
            more.addView(view);
            final int j = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                    intent.putExtra(Constants.KEY_GOODS_ID, info.get(j).goods_id);
                    startActivity(intent);

                    // Intent intent = new Intent(getActivity(), ProductDetailActivity1.class);
                    // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    // intent.putExtra("productId", info.get(j).goods_id);
                    // startActivity(intent);
                }
            });
        }
    }

    private String formatDaZhe(Float a) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(a);
    }

    private int getWidth() {
        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }


}
